﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Contoso.Models;
using Contoso.Helpers;

namespace Contoso.Controllers
{
    public class StudentController : Controller
    {
        private IUnitOfWork _unitOfWork;

        public StudentController(IUnitOfWork unitOfWork)
        {
            this._unitOfWork = unitOfWork;
        }


        public ActionResult Index()
        {
            return View(_unitOfWork.StudentsRepository.Students());
        }


        public ActionResult Details(int? id)
        {
            if (id == null)
                //throw new HttpException(404, "NOT FOUND");
                return new FileNotFoundResult { Message = "Invalid StudentID supplied" };

            var student = _unitOfWork.StudentsRepository.Students().Where(x => x.StudentID == id).FirstOrDefault();

            if (student == null)
                return new FileNotFoundResult { Message = "No Student found for that StudentID supplied" };

            return View(student);
        }


        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                _unitOfWork.StudentsRepository.Add(student);
                _unitOfWork.StudentsRepository.Save();

                return RedirectToAction("Details", new { id = student.StudentID });
            }

            return View(student);
        }



        public ActionResult NotFound()
        {
            return View();
        }


        public ActionResult Trouble()
        {
            return View("Error");
        }


    }
}
